/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package radio_station7.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author ebababi
 */
@Entity
@Inheritance
@DiscriminatorColumn(name="TYPE")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Album.findAll", query = "SELECT a FROM Album a"),
    @NamedQuery(name = "Album.findById", query = "SELECT a FROM Album a WHERE a.id = :id"),
    @NamedQuery(name = "Album.findByTitle", query = "SELECT a FROM Album a WHERE a.title = :title"),
    @NamedQuery(name = "Album.findByReleaseDate", query = "SELECT a FROM Album a WHERE a.releaseDate = :releaseDate"),
    @NamedQuery(name = "Album.findByFormat", query = "SELECT a FROM Album a WHERE a.format = :format"),
    @NamedQuery(name = "Album.findByDiskNumber", query = "SELECT a FROM Album a WHERE a.diskNumber = :diskNumber")})
public abstract class Album implements Serializable {
    @Transient
    protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(nullable = false)
    protected Long id;
    @Basic(optional = false)
    @Column(nullable = false, length = 255)
    protected String title;
    @Basic(optional = false)
    @Column(name = "RELEASE_DATE", nullable = false)
    @Temporal(TemporalType.DATE)
    protected Date releaseDate;
    @Basic(optional = false)
    @Column(nullable = false, length = 255)
    protected String format;
    @Basic(optional = false)
    @Column(name = "DISK_NUMBER", nullable = false)
    protected int diskNumber = 1;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "albumId")
    protected Collection<Song> songCollection;
    @JoinColumn(name = "COMPANY_ID", referencedColumnName = "ID", nullable = false)
    @ManyToOne(optional = false)
    protected ProductionCompany companyId;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        Long oldId = this.id;
        this.id = id;
        changeSupport.firePropertyChange("id", oldId, id);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        String oldTitle = this.title;
        this.title = title;
        changeSupport.firePropertyChange("title", oldTitle, title);
    }

    public Date getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(Date releaseDate) {
        Date oldReleaseDate = this.releaseDate;
        this.releaseDate = releaseDate;
        changeSupport.firePropertyChange("releaseDate", oldReleaseDate, releaseDate);
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        String oldFormat = this.format;
        this.format = format;
        changeSupport.firePropertyChange("format", oldFormat, format);
    }

    public int getDiskNumber() {
        return diskNumber;
    }

    public void setDiskNumber(int diskNumber) {
        int oldDiskNumber = this.diskNumber;
        this.diskNumber = diskNumber;
        changeSupport.firePropertyChange("diskNumber", oldDiskNumber, diskNumber);
    }

    @XmlTransient
    public Collection<Song> getSongCollection() {
        return songCollection;
    }

    public void setSongCollection(Collection<Song> songCollection) {
        this.songCollection = songCollection;
    }

    public ProductionCompany getCompanyId() {
        return companyId;
    }

    public void setCompanyId(ProductionCompany companyId) {
        ProductionCompany oldCompanyId = this.companyId;
        this.companyId = companyId;
        changeSupport.firePropertyChange("companyId", oldCompanyId, companyId);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Album)) {
            return false;
        }
        Album other = (Album) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return String.format("%s (%d)", title, releaseDate.getYear());
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }
    
}
